<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/21
 * Time: 17:33
 */

namespace app\index\controller;

use app\index\model\Room as Rooms;
use app\index\model\RoomCategory;
use app\index\model\User;
use think\Request;

class Room extends Base
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public function index()
    {
        $room = new Rooms();
        $key = input('get.key');
        $list = $room->alias('r')->join('user u', 'r.uid = u.id')->where('nickname|title', 'like', "%$key%")->order('r.status desc')->paginate(0,false,['query' => request()->param()]);
        $this->assign('key', $key);
        $this->assign('data', $list);
        return $this->fetch();
    }

    /**查询用户
     * @param $id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function show_user($id)
    {
        $list = [];
        if ($id) {
            $user = new User();
            $list = $user->get($id);
        }
        $this->assign('data', $list);
        return $this->fetch();
    }

    /**房间分类
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException']
     */
    public function category()
    {
        $category = new RoomCategory();
        $list = $category->select();
        $this->assign('data', attr($list,[1=>'启用',2=>'禁用']));
        return $this->fetch();
    }

    public function category_edit()
    {
        $id = input('get.id/d');
        $list['status'] = '';
        if ($id) {
            $category = new RoomCategory();
            $list = $category->get($id);
        }
        $this->assign('data', $list);
        return $this->fetch();
    }

    public function category_save()
    {
        $category = new RoomCategory();
        $data = input('post.');
        $id = isset($data['id']) ? $data['id'] : '';
        if ($id) {
            $data['update_time'] = time();
            $r = $category->update($data);
        } else {
            $r = $category->insert($data);
        }
        if ($r) {
            return resposer('', 1);
        }
        return resposer('修改失败', 0);
    }

    public function del_category($id)
    {
        $category = new RoomCategory();
        if ($category->where(['id' => $id])->delete()) {
            $r = resposer('删除成功', 1);
        } else {
            $r = resposer('删除失败', 0);
        }
        return $r;
    }


}